package strauss.flatland.view;

public class FPS {
	private long startTime;
	private long frameCount;
	private int targetFPS;
	
	public FPS(int targetFPS) {
		this.targetFPS=targetFPS;
	}
	
	public void reset() {
		startTime=System.currentTimeMillis();
		frameCount=0;
	}
	
	public void tick() {
		++frameCount;
		if (frameCount%targetFPS==0) {
			System.out.println(String.format("fps=%f", calcFPS(frameCount, startTime)));
			reset();
		}
	}
	
	private double calcFPS(long frameCount, long startTime) {
		double delta = (System.currentTimeMillis()-startTime)/1000.0;
		if (delta>0) {
			return frameCount/delta;
		} else {
			return -1.0;
		}
	}
}
